<template>
  <!-- 添加或修改参数配置对话框 -->
  <el-dialog :title="dataForm.id? $t('table.edit'): $t('table.create')" :visible.sync="visible" width="500px" append-to-body :close-on-click-modal="false" >
    <el-form ref="dataFormRef" :model="dataForm" :rules="rules" label-width="100px">
<#list formList as item>
      <#if item.htmlType == 'text'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-input v-model="dataForm.${item.attrName}" placeholder="${item.attrLabel!}"></el-input>
      </el-form-item>
      <#elseif item.htmlType == 'textarea'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-input type="textarea" v-model="dataForm.${item.attrName}"></el-input>
      </el-form-item>
      <#elseif item.htmlType == 'editor'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-input type="textarea" v-model="dataForm.${item.attrName}"></el-input>
      </el-form-item>
      <#elseif item.htmlType == 'select'>
      <#if item.dictType?? && item.dictType != "">
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <fast-select v-model="dataForm.${item.attrName}" dict-type="${item.dictType}" placeholder="${item.attrLabel!}"></fast-select>
      </el-form-item>
      <#else>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-select v-model="dataForm.${item.attrName}" placeholder="请选择">
          <el-option label="请选择" value="0"></el-option>
        </el-select>
      </el-form-item>
      </#if>
      <#elseif item.htmlType == 'radio'>
        <#if item.dictType?? && item.dictType != "">
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <fast-radio-group v-model="dataForm.${item.attrName}" dict-type="${item.dictType}"></fast-radio-group>
      </el-form-item>
        <#else>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-radio-group v-model="dataForm.${item.attrName}">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
        </#if>
      <#elseif item.htmlType == 'checkbox'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-checkbox-group v-model="dataForm.${item.attrName}">
          <el-checkbox label="启用" name="type"></el-checkbox>
          <el-checkbox label="禁用" name="type"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <#elseif item.htmlType == 'date'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-date-picker type="date" placeholder="${item.attrLabel!}" v-model="dataForm.${item.attrName}"></el-date-picker>
      </el-form-item>
      <#elseif item.htmlType == 'datetime'>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-date-picker type="datetime" placeholder="${item.attrLabel!}" v-model="dataForm.${item.attrName}"></el-date-picker>
      </el-form-item>
      <#else>
      <el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
        <el-input v-model="dataForm.${item.attrName}" placeholder="${item.attrLabel!}"></el-input>
      </el-form-item>
      </#if>
    </#list>
    </el-form>
    <div slot="footer" class="dialog-footer">
    <el-button @click="visible = false">{{ $t('table.cancel') }}</el-button>
    <el-button type="primary" @click="dataFormSubmit">{{ $t('table.confirm') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as ${beanName}Api from "@/api/${moduleName}/${beanName}";

export default {
  dicts: ['health_state'],
  data() {
    return {
      isCanSubmit: true,
      visible: false,
      dataForm: {
        <#list columnList as item>
        ${item.attrName}: undefined<#sep>,
        </#list>

      },
      // 表单校验
      rules: {
        <#list formList as item>
        <#if item.reqItem>
        ${item.attrName}: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]<#sep>,
        </#if>
        </#list>

      },
    }
  },
  created() {

  },
  methods: {
    init(id) {
      this.isCanSubmit = true
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataFormRef.resetFields()
        this.dataForm.id = id || null
        if (!this.dataForm.id) {
          return
        }
        ${beanName}Api.get(this.dataForm.id).then(res => {
          if (!res) {
          return
          }
          this.dataForm = res.data
          // 处理值类型不对导致的问题
          this.dataForm.state = res.data.state + "";
        })
      })
    },


    // 表单提交
    dataFormSubmit() {
      this.$refs.dataFormRef.validate(valid => {
        if (!valid) {
          return
        }
        this.isCanSubmit = false
        this.dataForm.id = this.dataForm.id<1 ? null : this.dataForm.id;
        const request = this.dataForm.id ? ${beanName}Api.update(this.dataForm) : ${beanName}Api.save(this.dataForm)
        request.then(data => {
          this.$message({
            message: this.$t('table.actionSuccess'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
              this.$refs.dataFormRef.resetFields()
            }
          })
        })
      })
    }
  }
}
</script>
